package com.libs.agricultural;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import com.libs.agricultural.R;
import com.libs.agricultural.AddressFragment.myThreadParam;
import com.libs.model.log;
import com.libs.model.parameter;
import common.HttpHelper;
import common.JSONHelper;
import common.Urls;
import common.Userinfo;

import everAdapter.CommonAdapter;
import everAdapter.ViewHolder;

import android.R.color;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;

public class Log extends Activity implements OnClickListener,SwipeRefreshLayout.OnRefreshListener {

	private SwipeRefreshLayout mSwipeLayout;
	private ListView mListView;

	private List<log> data;
	private CommonAdapter<log> adapter;
	private HttpHelper hlper;
	
	Calendar c = Calendar.getInstance();
	private int year = c.get(Calendar.YEAR);
	private int monthOfYear = c.get(Calendar.MONTH);
	private int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
	int month=monthOfYear + 1;
	private String timeNow= year + "-"+ (month<10?"0"+month:month) + "-" + (dayOfMonth<10?"0"+dayOfMonth:dayOfMonth);
	
	private TextView log_back;
	private TextView log_time;

	private Handler mHandler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			switch (msg.what)
			{
			case 1:
				data=(List<log>)msg.obj;
				adapter=new CommonAdapter<log>(Log.this, data, R.layout.log_itme) {	
					@Override
					public void convert(ViewHolder holder, log t) {
						holder.setText(R.id.log_username, t.getUsername());
						holder.setText(R.id.log_time, t.getTime());
						holder.setText(R.id.log_operate, t.getOperate());
						holder.setText(R.id.log_target, t.getTarget());
						holder.setText(R.id.log_parameter, t.getParameter());
					}
				};
				mListView.setAdapter(adapter);
				mSwipeLayout.setRefreshing(false);
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.log);
		SysApplication.getInstance().addActivity(this);
		ininView();
		ininEvent();
		log_time.setText(timeNow);
		mSwipeLayout.setColorSchemeResources(color.holo_green_dark, color.holo_green_light,
				color.holo_orange_light, color.holo_red_light);
		new Thread(new myThreadLog()).start();
	}
	private void ininView() {
		mListView = (ListView) findViewById(R.id.id_listview);
		mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
		log_back=(TextView) findViewById(R.id.log_back);
		log_time=(TextView) findViewById(R.id.log_time);
	}
	private void ininEvent() {
		mSwipeLayout.setOnRefreshListener(this);
		log_back.setOnClickListener(this);
		log_time.setOnClickListener(this);
	}
	@Override
	public void onRefresh() {
		if(data!=null){
			data.clear();
			adapter.notifyDataSetChanged();
		}
		new Thread(new myThreadLog()).start();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.log_back:
			finish();
			break;
		case R.id.log_time:
			showDialog();
			break;
		}
	}

	private void showDialog() {	
		DatePickerDialog date = new DatePickerDialog(this,
				new OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						int month=monthOfYear + 1;
						String timeNow= year + "-"+ (month<10?"0"+month:month) + "-" + (dayOfMonth<10?"0"+dayOfMonth:dayOfMonth);
						log_time.setText(timeNow);
						timeNow=log_time.getText().toString();
						if(data!=null){
							data.clear();
							adapter.notifyDataSetChanged();
						}
						new Thread(new myThreadLog()).start();
					}
				}, year, monthOfYear, dayOfMonth);
		date.show();
	}
	
	class myThreadLog implements Runnable {   
		public void run() {  
	    	hlper=new HttpHelper();
	    	List<NameValuePair> parn=new ArrayList<NameValuePair>();
	    	parn.add(new BasicNameValuePair("type", "SelectByTime"));
	    	parn.add(new BasicNameValuePair("id", Userinfo.num+""));
	    	parn.add(new BasicNameValuePair("time",log_time.getText().toString()));
	    	JSONArray json=hlper.HttpList(Urls.LogUrl, parn);
	    	if(json!=null){
	    		log[] uu=null;
	    		try {
					uu=JSONHelper.parseArray(json, log.class);
					List<log> list=new ArrayList<log>();
					for(int i=0;i<uu.length;i++){
						list.add(uu[i]);
						android.util.Log.i("lbs", uu[i].getId()+"");
					}
					Message message = new Message();   
			        message.what = 1;
			        message.obj=list;
			        mHandler.sendMessage(message);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    	}
	    }   
	}
}
